<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class San_pham extends Base_controller {

    var $limit = 20;

    public function __construct() {
        parent::__construct();
        $this->check_login();
    }

    public function tat_ca_sp() {
        $this->load->model("com_model");
        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("SanPham");

        $total_seg = $this->uri->total_segments();
        $data["stt"] = $this->uri->segment(4);
        $data["cateID"] = $this->uri->segment(5);
//config pagination

        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $TSP = $this->input->post("txtName", TRUE);
            }
            if ($this->input->get("pg") === "search") {
                $TSP = ($this->input->get("name", TRUE)) ? $this->input->get("name", TRUE) : NULL;
            }
            $base_url = base_url() . "dienhoa-administrator/san_pham/tat_ca_sp/?pg=search&MHD=" . $TSP;
            if (isset($data["stt"]) && $data["stt"] != NULL)
                $result = $this->product_model->QlAllSp(NULL, $data["stt"], $this->limit, $this->input->get("page"));
            if (isset($data["cateID"]) && $data["cateID"] != NULL)
                $result = $this->product_model->QlAllSp(NULL, NULL, $data["cateID"], $this->limit, $this->input->get("page"));
            else
                $result = $this->product_model->QlAllSp($TSP, NULL, NULL, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {
            $base_url = base_url() . "dienhoa-administrator/san_pham/tat_ca_sp/?pg=hd";
            if (isset($data["stt"]) && $data["stt"] != NULL)
                $result = $this->product_model->QlAllSp(NULL, $data["stt"], NULL, $this->limit, $this->input->get("page"));
            if (isset($data["cateID"]) && $data["cateID"] != NULL)
                $result = $this->product_model->QlAllSp(NULL, NULL, $data["cateID"], $this->limit, $this->input->get("page"));
            else
                $result = $this->product_model->QlAllSp(NULL, NULL, NULL, $this->limit, $this->input->get("page"));
        }

        $data["san_pham"] = $result["rows"]->result();
        $data['num_results'] = $result['num_rows'];
        $data["danh_muc"] = $this->product_model->GetCategory(NULL)->result();
        
       
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/products/tat_ca_sp_view", $data, TRUE));
    }

    public function xoa() {
        $this->permission_del_edit();
        $dat = $this->input->post("item");
        for ($i = 0; $i < sizeof($dat); $i++) {
            $delete = $this->product_model->DeleteSP($dat[$i]);
            if (!$delete) {
                echo "<script>alert('Không xóa được!'); window.history.back();</script>";
            }
        }
        redirect("dienhoa-administrator/san_pham/tat_ca_sp", "refresh");
    }

    public function chi_tiet_san_pham() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("SanPham");
        $fid = $this->uri->segment(4);
        $data["sp_view"] = $this->product_model->getProduct($fid, NULL)->row();
        $data["cat_view"] = $this->product_model->GetCategory(NULL)->result();
        $data["m_menu"] = $this->menu_model->getMenu()->result();
        $this->render($this->load->view("admin/products/chi_tiet_sp_view", $data, TRUE));
    }

    public function sua_san_pham() {
        $this->permission_del_edit();
        $fid = $this->uri->segment(4);
        $data = array();
        //kiem tra truong nhap lieu
        $config = array(
            array(
                'field' => 'txtName',
                'label' => 'Tên sản phẩm',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'optCat',
                'label' => 'Nhóm sản phẩm',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optMenu',
                'label' => 'Menu',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'txtPrice',
                'label' => 'Giá sản phẩm',
                'rules' => 'required|numeric|xss_clean|trim'
            ),
            array(
                'field' => 'optNewPd',
                'label' => 'Sản phẩm mới',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optBestSell',
                'label' => 'Sản phẩm bán chạy',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optMostView',
                'label' => 'Sản phẩm xem nhiều',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'PdStatus',
                'label' => 'Trạng thái sản phẩm',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'intro',
                'label' => 'Nội dung sản phẩm',
                'rules' => 'trim|required'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === false) {
            $this->chi_tiet_san_pham();
        } else {
            $txtName = $this->input->post('txtName');
            $optCat = $this->input->post('optCat');
            $m = $this->input->post("optMenu");
            $txtPrice = $this->input->post("txtPrice");
            $optNewPd = $this->input->post('optNewPd');
            $optBestSell = $this->input->post('optBestSell');
            $optMostView = $this->input->post('optMostView');
            $PdStatus = $this->input->post('PdStatus');
            $content = $this->input->post('intro');

            if ($_FILES['file_img']['name'] != '') {
                $config['upload_path'] = './images/sanpham';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['remove_spaces'] = TRUE;
                $config['overwrite'] = TRUE;
                $config['file_name'] = $_FILES['file_img']['name'];
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload("file_img")) {
                    return false;
                } else {
                    $upload = $this->upload->data();
                    $img = $upload["file_name"];
                    $data["upSP"] = $this->product_model->UpdatePd($fid, $txtName, $optCat,$m, $txtPrice, $content, $optNewPd, $optBestSell, $optMostView, $PdStatus, $img);
                    if ($data["upSP"]) {
                        redirect("dienhoa-administrator/san_pham/tat_ca_sp", "refresh");
                    } else {
                        echo "<script>alert('Sửa thất bại, xin hãy thử lại!');window.history.back();</script>";
                    }
                }
            } else {
                $row = $this->product_model->getProduct($fid, NULL)->row();
                if ($row->FPicture) {
                    $img = $row->FPicture;
                } else {
                    $img = "";
                }
                $data["upSP"] = $this->product_model->UpdatePd($fid, $txtName, $optCat,$m, $txtPrice, $content, $optNewPd, $optBestSell, $optMostView, $PdStatus, $img);
                if ($data["upSP"]) {
                    redirect("dienhoa-administrator/san_pham/tat_ca_sp", "refresh");
                } else {
                    redirect("dienhoa-administrator/san_pham/chi_tiet_san_pham/$fid");
                }
            }
        }
    }
    //form them san pham
    public function them_sp(){
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("SanPham");
        $data["cat_view"] = $this->product_model->GetCategory(NULL)->result();
        $data["m_menu"] = $this->menu_model->getMenu()->result();
        $this->render($this->load->view("admin/products/them_san_pham_view",$data,TRUE));
    }
    
    //them san pham
    public function xu_ly_them_sp() {
        $config = array(
            array(
                'field' => 'txtName',
                'label' => 'Tên sản phẩm',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'optCat',
                'label' => 'Nhóm sản phẩm',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optMenu',
                'label' => 'Menu',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'txtPrice',
                'label' => 'Giá sản phẩm',
                'rules' => 'required|numeric'
            ),
            array(
                'field' => 'optNewPd',
                'label' => 'Sản phẩm mới',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optBestSell',
                'label' => 'Sản phẩm bán chạy',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'optMostView',
                'label' => 'Sản phẩm xem nhiều',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'PdStatus',
                'label' => 'Trạng thái sản phẩm',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'intro',
                'label' => 'Nội dung sản phẩm',
                'rules' => 'trim|required'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() === false) {
            $this->them_sp();
        } else {
            $query_array = array(
                "txtName" => $this->input->post('txtName'),
                "optCat" => $this->input->post('optCat'),
                "m" => $this->input->post("optMenu"),
                "txtPrice" => $this->input->post("txtPrice"),
                "optNewPd" => $this->input->post('optNewPd'),
                "optBestSell" => $this->input->post('optBestSell'),
                "optMostView" => $this->input->post('optMostView'),
                "PdStatus" => $this->input->post('PdStatus'),
                "content" => $this->input->post('intro')
            );
            $query_array["slug"] = mb_strtolower(url_title(removesign($query_array["txtName"])));
            if ($_FILES['file_img1']['name'] != '') {
                $config['upload_path'] = './images/sanpham';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['remove_spaces'] = TRUE;
                $config['file_name'] = $_FILES['file_img1']['name'];
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload("file_img1")) {
                    return false;
                } else {
                    $upload = $this->upload->data();
                    $query_array["img"] = $upload["file_name"];
                    $data["ThemSP"] = $this->product_model->add($query_array);
                    if ($data["ThemSP"]) {
                        redirect("dienhoa-administrator/san_pham/tat_ca_sp", 'refresh');
                    } else {
                        echo "<script>alert('khong them dc');</script>";
                        $this->them_sp();
                    }
                }
            } else {
                $query_array["img"] = "";
                $data["ThemSP"] = $this->product_model->add($query_array);
                if ($data["ThemSP"]) {
                    redirect("dienhoa-administrator/san_pham/tat_ca_sp", 'refresh');
                } else {
                    $this->them_sp();
                }
            }
        }
    }

}

?>
